package me.zd.tool.hardlink.view;

import me.zd.tool.hardlink.view.listener.*;

import javax.swing.*;
import javax.swing.text.JTextComponent;
import java.util.function.Consumer;

public class ModelBinder {

    public static void bind(JTextComponent component, Consumer<String> consumer, ModelLock modelLock) {
        TextChangedListener listener = new TextChangedListener(component, consumer, modelLock);
        component.getDocument().addDocumentListener(listener);
    }

    public static void bind(JSpinner component, Consumer<Number> consumer, ModelLock modelLock) {
        NumberSpinnerListener listener = new NumberSpinnerListener(component, consumer, modelLock);
        component.addChangeListener(listener);
    }

    public static <T> void bind(JComboBox<T> component, Consumer<T> consumer, ModelLock modelLock) {
        ComboBoxChangedListener<T> listener = new ComboBoxChangedListener<>(component, consumer, modelLock);
        component.addItemListener(listener);
    }

    public static void bind(JCheckBox component, Consumer<Boolean> consumer, ModelLock modelLock) {
        CheckBoxChangedListener listener = new CheckBoxChangedListener(component, consumer, modelLock);
        component.addItemListener(listener);
    }
}
